<?php include 'header.php'; ?>
<?php include '../settings.php'; ?>
<?php

$con=mysqli_connect($host,$dbuserW,$dbpassW,$dbname);

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//var_dump($_POST);



If (strlen($_POST['NewCategory'])>0) 
{

$Catdesc=$_POST['NewCategory'];
$tCat=mysqli_query($con,"Select UUID() as newval;");
//var_dump($tCat);
//$ttCat=mysql_fetch_assoc($tCat);
//var_dump($ttCat);
//$Cat=$ttCat["newval"];
while($row = mysqli_fetch_array($tCat)) 
{
//echo("<br>");
//echo($row[newval]."<br>");
$Cat=$row[newval];
}

echo("NewCategory=".$Cat);
$theinsert="Insert into Category Value ('".$Cat."','".$Catdesc."','1','','0');";
$res=mysqli_query($con,$theinsert);
//echo($theinsert);
$errors=mysqli_error($con);
echo("<br>Errorno=".$errors."<br>");
if (mysqli_errno($con)) 
{
echo("-------Failed Insert------");
$Cat="";
}
}
//;return link
//echo("<a href='".$adminlocation."sura.php?Sura=".$Sura."#".$firstayavals."'>Back to Form</a>");


//mysqli_close($con);



foreach ($_POST['Cat'] as $catid=>$delpair)
{
$arraydelpair=explode(",",$delpair);
//$catid=$arraydelpair[0];
$parentid=$arraydelpair[0];
echo $catid.":".$parentid."<br>";
//if ($parentid==0) $parentid="";
$qr1="update Category set ParentIndex='".$parentid."' where CIndex='".$catid."';";
$newCatdone=mysqli_query($con,$qr1);
echo $qr1."<br>";
//echo "dd";
}

foreach ($_POST['desc'] as $catid=>$delpair)
{
$arraydelpair=explode(",",$delpair);
//$catid=$arraydelpair[0];
$newdesc=$arraydelpair[0];
echo $catid.":".newdesc."<br>";
//if ($parentid==0) $parentid="";
$qr1="update Category set Description='".$newdesc."' where CIndex='".$catid."';";
$newCatdone=mysqli_query($con,$qr1);
//echo $qr1."<br>";
//echo "dd";
}

foreach ($_POST['ldesc'] as $catid=>$delpair)
{
$arraydelpair=explode(",",$delpair);
//$catid=$arraydelpair[0];
$newdesc=$arraydelpair[0];
echo $catid.":".$newdesc."<br>";
//if ($parentid==0) $parentid="";
$qr1="update Category set LongDescription='".$newdesc."' where CIndex='".$catid."';";
$newCatdone=mysqli_query($con,$qr1);
echo $qr1."<br>";
//echo "dd";
}

if ($_POST['delete']=='delete')
{
foreach ($_POST['CatDel'] as $catid=>$delpair)
{
echo "Delete:".$catid."<br>";

$delparent="update Category set ParentIndex='' where ParentIndex='".$catid."';";
$delchildren="delete from  CategoryAya  where CategoryIndex='".$catid."';";
$delcat="delete from Category Where CIndex='".$catid."';";
echo $delparent."<br>";
echo $delchildren."<br>";
echo $delcat."<br>";

$res=mysqli_query($con,$delparent);
var_dump($res);
echo "<br>";
$res=mysqli_query($con,$delchildren);
var_dump($res);
echo "<br>";
$res=mysqli_query($con,$delcat);
var_dump($res);
echo "<br>";

}
}



?> 
<?php include 'footer.php'; ?>